package com.example.myapp;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.db.DBUtils;
import com.example.myapp.db.UserSQLiteOpenHelper;
import com.example.myapp.entity.User;
import com.example.myapp.utils.Constants;
import com.example.myapp.utils.Tools;

public class RegisterActivity extends AppCompatActivity  implements View.OnClickListener {
    private  ImageView register_back_img;
    private  EditText register_username_et;
    private  EditText register_password_et;
    private  EditText register_confirm_password_et;
    private  EditText register_nickname_tv;
    private  RadioGroup register_gender_rg;
    private  EditText register_age_et;
    private  EditText register_phone_et;
    private  EditText register_email_et;
    private  Button register_submit_bt;
    private String username;
    private String password;
    private String nickname;
    private Integer gender;
    private Integer age;
    private String phone;
    private String email;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_layout);
         register_back_img = findViewById(R.id.register_back_img);
        register_username_et = findViewById(R.id.register_username_et);
         register_password_et = findViewById(R.id.register_password_et);
        register_confirm_password_et = findViewById(R.id.register_confirm_password_et);
        register_nickname_tv = findViewById(R.id.register_nickname_et);
        register_gender_rg = findViewById(R.id.register_gender_rg);
//        register_gender_rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup radioGroup, int i) {
//                //i=1即选了男  i=2即选了女
//                gender=i;
//                Log.i("RegisterActivity","选中了："+i);
//            }
//        });
         register_age_et = findViewById(R.id.register_age_et);
         register_phone_et = findViewById(R.id.register_phone_et);
         register_email_et = findViewById(R.id.register_email_et);
        register_submit_bt = findViewById(R.id.register_submit_bt);

        register_back_img.setOnClickListener(this);
        register_submit_bt.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

            case R.id.register_back_img:
                finish();
                break;
            case R.id.register_submit_bt:
                username = register_username_et.getText().toString().trim();
                password=register_password_et.getText().toString();
                nickname=register_nickname_tv.getText().toString().trim();
                gender=register_gender_rg.getCheckedRadioButtonId();
                String ageStr = register_age_et.getText().toString();
                if (Tools.isNotEmpty(ageStr)) {
                    age = Integer.parseInt(ageStr);
                }
                 phone=register_phone_et.getText().toString();
                 email=register_email_et.getText().toString();
                 if (!submitCheck()){//校验所填选项的值
                     return;
                 }
                 //封装用户信息
                User user = new User();
                 user.setUsername(username);
                 user.setPassword(password);
                 user.setNickname(nickname);
                 user.setGender(gender);
                 user.setAge(age);
                 user.setPhone(phone);
                 user.setEmail(email);
                 //插入用户数据
                boolean insertUserSuccess = insertUserInfo(user);
                if (insertUserSuccess){//如果成功
                    //跳到成功页
                    Intent intent = new Intent(RegisterActivity.this, RegisterSuccessActivity.class);
                    startActivity(intent);
                    //关闭当前页面
                    finish();
                }
                break;
        }
    }

    private boolean insertUserInfo(User userInfo) {
        UserSQLiteOpenHelper userSQLiteOpenHelper = new UserSQLiteOpenHelper(RegisterActivity.this, Constants.DATABASE_NAME);
        SQLiteDatabase db = userSQLiteOpenHelper.getWritableDatabase();
        //注册插入之前先检测当前用户名、电话、邮箱是否被注册
        boolean existUser = DBUtils.isExistUser(RegisterActivity.this, db, userInfo.getUsername(), userInfo.getPhone(), userInfo.getEmail());
        if (existUser){//返回true说明已经存在
            db.close(); //关闭连接数据库
            return false; // 不能往下执行了->返回失败
        }

        //创建ContentValues对象
        ContentValues insertValues = new ContentValues();
        //封装用户信息
        insertValues.put("username",userInfo.getUsername());
        insertValues.put("password",userInfo.getPassword());
        insertValues.put("nickname",userInfo.getNickname());
        insertValues.put("gender",userInfo.getGender());
        insertValues.put("age",userInfo.getAge());
        insertValues.put("phone",userInfo.getPhone());
        insertValues.put("email",userInfo.getEmail());
        // 插入数据方式一：调用insert()方法将数据插入到数据库当中
        db.insert("user", null, insertValues);
        //关闭数据库连接
        db.close();

        return true;

//        //插入数据方式二：写insert SQL语句
//        String insertUserInfoSQL= "insert into user (username,password,nickname,gender,age,phone,email) " +
//                "values ("+userInfo.getUsername()+","
//                +userInfo.getPassword() +","
//                +userInfo.getNickname() +","
//                +userInfo.getGender()+","
//                +userInfo.getAge()+","
//                +userInfo.getPhone()+","
//                +userInfo.getEmail()+
//                ")";
//       db.execSQL(insertUserInfoSQL);
    }

    //检测选项的值
    private boolean submitCheck(){
        if (Tools.isEmpty(username)){
            Tools.toastTip(RegisterActivity.this,"用户名不能为空！");
              return false;
        }
        if (Tools.isEmpty(password)){
            Tools.toastTip(RegisterActivity.this,"密码不能为空！");
            return false;
        }
        String confirmPassword = register_confirm_password_et.getText().toString();

        if (Tools.isEmpty(confirmPassword)){
            Tools.toastTip(RegisterActivity.this,"确认密码不能为空！");
            return false;
        }
        if (!password.equals(confirmPassword)){
            Tools.toastTip(RegisterActivity.this,"密码不一致！");
            return false;
        }

        if (gender==-1){
            Tools.toastTip(RegisterActivity.this,"请选择性别！");
            return false;
        }
        String ageStr = register_age_et.getText().toString();
        if (Tools.isEmpty(ageStr)){
            Tools.toastTip(RegisterActivity.this,"年龄不能为空！");
            return false;
        }

        if (Tools.isEmpty(phone)){
            Tools.toastTip(RegisterActivity.this,"手机号不能为空！");
            return false;
        }
        if (Tools.isEmpty(email)){
            Tools.toastTip(RegisterActivity.this,"邮箱不能为空！");
            return false;
        }
        return true;
    }









}
